#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys

from fabric.api import *
from fabric.contrib.files import exists

BASE_PATH = '/home/xu.du/apps/'
APP_PATH = BASE_PATH + 'office-price'
APP_NAME = ''


@parallel(pool_size=30)
def deploy_app():
    if not exists(APP_PATH):
        run('mkdir -p ' + APP_PATH)
    with settings(warn_only=True):
        sudo('rm -Rf ' + APP_PATH + '/*')
    put('*.py', APP_PATH + '/')


@parallel(pool_size=30)
def run_app():
    with cd(APP_PATH):
        run('source sc_env/bin/activate && $(nohup python {app} >& /dev/null < /dev/null &) && sleep 1'.format(app=APP_NAME), shell='/bin/bash')


@parallel(pool_size=30)
def stop():
    with settings(warn_only=True):
        run('pkill -f ' + APP_NAME)


@parallel(pool_size=30)
def test():
    with settings(warn_only=True):
        run('pip list | grep ' + sys.argv[1])
        # sudo('pip install xtls==0.4.1')


if __name__ == "__main__":
    env.hosts = []
    env.password = 'qwerfdsa'
    execute(deploy_app)
    # execute(run_app)
    # execute(stop)
    # execute(test)
